package com.net355.eblog.controller;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.net355.eblog.models.EbUser;


public class BaseController {
	@Autowired
	protected ServletContext context;
	protected static final int DEFAULT_PAGESIZE = 20;


	/**
	 * returns the current http session object
	 * 
	 * @return session
	 */
	public HttpSession getSession() {
		ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
		HttpSession contextSess = attr == null ? null : attr.getRequest().getSession(true);
		return contextSess;
	}

	/**
	 * returns the current http session object
	 * 
	 * @return session
	 */
	public HttpServletRequest getRequest() {
		ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
		HttpServletRequest contextReq = attr == null ? null : attr.getRequest();
		return contextReq;
	}

	/**
	 * 得到当前登陆用户
	 * 
	 * @return
	 */
	protected EbUser getCurrentUser() {
		EbUser user = (EbUser) getSession().getAttribute("SESSIONUSER");
		return user;
	}
	
	/**
	 * 得到当前登陆用户的所在部门
	 * 
	 * @return
	 */
	protected Integer getCurrentSector() {
		EbUser user = getCurrentUser();
		return user.getBelongSector();
	}
	
}
